using UnityEngine;


public class PlayerController : Singleton<PlayerController>
{
    [SerializeField] private float moveSpeed = 1f;
    public GameObject weaponPrimary;
    public GameObject weaponUpgrade;
    public bool isAttacking;

    private GameObject weaponCollider;

    private Vector2 movement;
    private Rigidbody2D rb;
    private Animator animator;
    private float moveX;
    private float moveY;

    protected override void Awake()
    {
        base.Awake();
        rb = GetComponent<Rigidbody2D>();
        animator = GetComponent<Animator>();
        if(GameManager.GetInstance().playerWeapon == PlayerWeapon.PrimaryWeapon)
        {
            weaponPrimary.SetActive(true);
            weaponUpgrade.SetActive(false);
            weaponCollider = weaponPrimary.transform.GetChild(0).gameObject;
        }
        if (GameManager.GetInstance().playerWeapon == PlayerWeapon.UpgradeWeapon)
        {
            weaponPrimary.SetActive(false);
            weaponUpgrade.SetActive(true);
            weaponCollider = weaponUpgrade.transform.GetChild(0).gameObject;
        }
    }

    public void UpgradeWeapon()
    {
        GameManager.GetInstance().playerWeapon = PlayerWeapon.UpgradeWeapon;
        weaponPrimary.SetActive(false);
        weaponUpgrade.SetActive(true);
        weaponCollider = weaponUpgrade.transform.GetChild(0).gameObject;
    }

    private void Update()
    {
        if (IsPlayerHurtingOrDying())
        {
            return;
        }

        PlayerInput();
    }

    private void FixedUpdate()
    {
        if (IsPlayerHurtingOrDying())
        {
            return;
        }

        FlipPlayer();
        Move();
    }

    private void PlayerInput()
    {
        moveX = Input.GetAxisRaw("Horizontal");
        moveY = Input.GetAxisRaw("Vertical");
        movement = new Vector2(moveX, moveY);
        if ((moveX == 0 && moveY == 0) || isAttacking)
        {
            animator.SetBool("Walking", false);
        }
        if (moveX != 0 || moveY != 0)
        {
            if (isAttacking)
            {
                return;
            }
            animator.SetBool("Walking", true);
        }
    }

    private void Move()
    {
        if (IsAttack())
        {
            return;
        }
        
        if (moveX != 0 || moveY != 0)
        {        
            rb.MovePosition(rb.position + movement * (moveSpeed * Time.fixedDeltaTime));      
        } 
    }

    private void FlipPlayer()
    {
        if (IsAttack())
        {
            return;
        }
        if (moveX < 0)
        {
            transform.localEulerAngles = new Vector3(0, 181, 0);
        }
        if (moveX > 0)
        {
            transform.localEulerAngles = new Vector3(0, 0, 0);
        }
    }


    private bool IsAttack()
    {
        AnimatorStateInfo stateInfo = animator.GetCurrentAnimatorStateInfo(0);
        if (stateInfo.IsName("Attack"))
        {
            return true;
        }
        return false;
    }


    public void Attack()
    {
        animator.SetTrigger("Attack");
    }

    private void SetWeaponColliderNotActive()
    {
        weaponCollider.SetActive(false);
    }

    private void SetWeaponColliderActive()
    {
        weaponCollider.SetActive(true);
    }

    private bool IsPlayerHurtingOrDying()
    {
        if (animator.GetBool("Dying"))
        {
            return true;
        }
        if (animator.GetBool("Hurting"))
        {
            return true;
        }
        return false;
    }

}
